package br.com.sipsoft.svola.dirf;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class Arquivo {

	public static List<String> lerTXT(String caminhoArquivo) {

		try {
			FileReader arq = new FileReader(new File(
					caminhoArquivo));
			BufferedReader lerArq = new BufferedReader(arq);

			String linha = lerArq.readLine(); 
			List<String> retorno = new ArrayList<String>();
			while (linha != null) {

				if (linha.toString().trim().length() > 0) {
					retorno.add(normalize(linha.toString()).trim());

				}
				linha = lerArq.readLine(); 
			}

			arq.close();

			return retorno;
		} catch (IOException e) {
			System.err.printf("Erro na abertura do arquivo: %s.\n",
					e.getMessage());
			return null;
		}

	}

	
	private static final char[] FIRST_CHAR = (" !'#$%&'()*+\\-./0123456789:;<->?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
			+ "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ E ,f'.++^%S<O Z  ''''.-"
			+ "-~Ts>o ZY !C#$Y|$'(a<--(_o+23'u .,1o>113?AAAAAAACEEEEIIIIDNOO"
			+ "OOOXOUUUUyTsaaaaaaaceeeeiiiidnooooo/ouuuuyty").toCharArray();

	private static final char[] SECOND_CHAR = ("  '         ,                                               "
			+ "\\                                   $  r'. + o  E      ''  "
			+ "  M  e     #  =  'C.<  R .-..     ..>424     E E            "
			+ "   E E     hs    e e         h     e e     h ").toCharArray();

	private static String normalize(String str) {
		char[] chars = str.toCharArray();
		StringBuffer ret = new StringBuffer(chars.length * 2);
		for (int i = 0; i < chars.length; ++i) {
			char aChar = chars[i];
			if (aChar == ' ' || aChar == '\t') {
				ret.append(' ');
			} else if (aChar > ' ' && aChar < 256) {
				if (FIRST_CHAR[aChar - ' '] != ' ') {
					ret.append(FIRST_CHAR[aChar - ' ']);
				}
				if (SECOND_CHAR[aChar - ' '] != ' ') {
					ret.append(SECOND_CHAR[aChar - ' ']);
				}
			}
		}

		return ret.toString();
	}

}
